package com.example.jt_sso.service;

import com.example.jt_common.RedisService;
import com.example.jt_sso.dao.TbuserMapper;
import com.example.jt_sso.pojo.Tbuser;
import com.example.jt_sso.pojo.TbuserExample;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;

import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    TbuserMapper tbuserMapper;

    @Autowired
    RedisService redisService;

    @Autowired
    ObjectMapper mapper ;

    private static final Integer time = 60*30;
/*
* type : 1 username 2 phone 3 emial
* */
    @Override
    public boolean checkUserExist(String param, int type) {

        if (type!=1||type!=2||type!=3){
            return false;
        }

        TbuserExample tbuserExample=new TbuserExample();

        if (type==1){
            tbuserExample.createCriteria().andUsernameEqualTo(param);
        }else if (type==2){
            tbuserExample.createCriteria().andPhoneEqualTo(param);
        }else if (type==3){
            tbuserExample.createCriteria().andEmailEqualTo(param);
        }

        List<Tbuser> tbusers = tbuserMapper.selectByExample(tbuserExample);

        if (tbusers.size()==0){
            return false;
        }else {
            return true;
        }

    }

    @Override
    public boolean userRegister(Tbuser user) {
        //验证用户是否存在
        boolean isExist = checkUserExist(user.getUsername(),1);
        if (!isExist){
            tbuserMapper.insert(user);
            return true;
        }

        return false;
    }

    @Override
    public String userLogin(String username, String password) {
        TbuserExample tbuserExample=new TbuserExample();
        tbuserExample.createCriteria().andUsernameEqualTo(username);
        List<Tbuser> tbusers = tbuserMapper.selectByExample(tbuserExample);
        if (tbusers.size()==0){
            return null;
        }else {
            if (tbusers.get(0).getPassword().equals(password)){
                //simplehash  shiro
                //ticket：md5类型的字符串，唯一性。
                String ticket = String.valueOf(DigestUtils.md5Digest((System.currentTimeMillis()+username+tbusers.get(0).getId()).getBytes()));
                Tbuser user = tbusers.get(0);
                //将user变为json字符串，存入redis中
                try {
                    String jsonUser = mapper.writeValueAsString(user);
                    redisService.setDataAndTime("TICKET_"+ticket,jsonUser,time);//应该加入reids中key的生存时间
                    return ticket;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                    return null;
                }

            }
        }
        return null;
    }

    @Override
    public String sessionLogin(String ticket) {
        String jsonUser = redisService.getDataByKey("TICKET_"+ticket);
        if (jsonUser==null){
            return null;
        }
        return jsonUser;
    }
}
